package com.zahb.qadx.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import com.google.gson.JsonObject;
import com.zahb.qadx.R;
import com.zahb.qadx.base.BaseActivity;
import com.zahb.qadx.net.RetrofitService;
import com.zahb.qadx.ui.fragment.CreateChoiceExamFragment;
import com.zahb.qadx.ui.fragment.CreateChoiceExamFragment2;
import com.zahb.qadx.util.Constant;
import com.zahb.qadx.util.JsonUtil;
import com.zahb.qadx.util.Tips;

import io.reactivex.android.schedulers.AndroidSchedulers;

public class ViewExamDetailsActivity extends BaseActivity {

    public ViewPager2 mViewPager2;
    private Fragment[] mFragments = new Fragment[2];

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitleText("查看试卷");
        getExamInfo(getIntent().getIntExtra("examId", -1));
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_view_exam_details;
    }

    @Override
    protected void initViews() {
        mViewPager2 = findViewById(R.id.view_pager2);
        mViewPager2.setOffscreenPageLimit(mFragments.length - 1);
        mViewPager2.setUserInputEnabled(false);
        mViewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                if (position == 0) {
                    setTitleText("查看试卷");
                } else if (position == 1) {
                    setTitleText("查看抽考");
                }
            }
        });
    }

    @Override
    protected int getTitleStringRes() {
        return R.string.exam_details;
    }

    private void getExamInfo(int examId) {
        showProgressDialog("请稍等...");
        addDisposable(RetrofitService.getNetService()
                .getExamInfo(examId)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(response -> {
                    if (response.getStatusCode() == Constant.CODE_SUCCESS) {
                        String json = JsonUtil.getGson().toJson(response.getData());
                        JsonObject jsonObject = JsonUtil.getParser().parse(json).getAsJsonObject();
                        int examPaperId = jsonObject.get("examPaperId").getAsInt();
                        getExamPaperInfo(examPaperId, json);
                    } else {
                        hideProgressDialog();
                        if (response.getStatusCode() != Constant.CODE_OFFLINE) {
                            showBindToast(response.getErrorInfo());
                        }
                    }
                }, throwable -> {
                    hideProgressDialog();
                    throwable.printStackTrace();
                    Tips.RequestError(getActivity());
                }));
    }

    private void getExamPaperInfo(int examPaperId, String json) {
        addDisposable(RetrofitService.getNetService()
                .getExamPaperInfo(examPaperId)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(response -> {
                    hideProgressDialog();
                    if (response.getStatusCode() == Constant.CODE_SUCCESS) {
                        String paperJson = JsonUtil.getGson().toJson(response.getData());
                        CreateChoiceExamFragment fragment = CreateChoiceExamFragment.newInstance(paperJson);
                        mFragments[0] = fragment;
                        CreateChoiceExamFragment2 fragment2 = CreateChoiceExamFragment2.newInstance(json, 0);
                        mFragments[1] = fragment2;
                        mViewPager2.setAdapter(new ExamFragmentAdapter(getActivity(), mFragments));
                    } else if (response.getStatusCode() != Constant.CODE_OFFLINE) {
                        showBindToast(response.getErrorInfo());
                    }
                }, throwable -> {
                    hideProgressDialog();
                    throwable.printStackTrace();
                    Tips.RequestError(getActivity());
                }));
    }

    public static void actionStart(Context context, int examId) {
        Intent intent = new Intent(context, ViewExamDetailsActivity.class);
        intent.putExtra("examId", examId);
        context.startActivity(intent);
    }

    public static class ExamFragmentAdapter extends FragmentStateAdapter {

        private Fragment[] mFragments;

        ExamFragmentAdapter(@NonNull FragmentActivity fragmentActivity, Fragment[] fragments) {
            super(fragmentActivity);
            mFragments = fragments;
        }

        @NonNull
        @Override
        public Fragment createFragment(int position) {
            return mFragments[position];
        }

        @Override
        public int getItemCount() {
            return mFragments.length;
        }
    }
}
